<?php
/**
 * @copyright meila.com
 * @author wangtao5@
 * @name 发布接口
 * @param
 *         $nick = 作用
 *
 *
 */
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_content.php');
class aj_albumfeed_publish extends ml_controller
{
    private $_gd_is_full=0; //宝贝信息是否完整
    private $_uid;
    //执行默认检查的参数集合
    protected $_param_check = array(
            'class_id'    => array('type'=>ML_DATATYPE_DIGIT, 'is_must'=>true),
            'album_id'    => array('type'=>ML_DATATYPE_DIGIT, 'is_must'=>true),    
            'pic_id'      => array('type'=>ML_DATATYPE_DIGIT, 'is_must'=>true),    
            'content_rid' => array('type'=>ML_DATATYPE_ALNUM, 'len_limit'=>array('eq'=>ML_RID_LEN)),
            'gd_info'     => array('call'=>'callback'),
            'content'     => array('call'=>'callback','is_must'=>true),
    );
    //错误描述
    protected $_error_msg = array(
            'class_id' => 'class_id',
            'album_id' => 'album_id',
            'pic_id' => 'pic_id',
            'content' => 'content',
            'gd_info' => 'gd_info',
            'content_rid' => 'content_rid',
    );
    private $_at_nick;
    private $_feed_rid;
    private $_content_type;
    /**
     * 初始化参数
     */
    function initParam()
    {
        //必选项
        $this->_option['content'] = Tool_input::input('content', 'P', '');
        $this->_option['class_id'] = Tool_input::input('class_id', 'P',0);
        $this->_option['album_id'] = Tool_input::input('album_id', 'P', 0);
        $this->_option['pic_id'] =  Tool_input::input('pic_id', 'P', 0);
        
        //宝贝信息
        $this->_option['gd_info']['gd_title'] = Tool_input::input('gd_title', 'P', '');
        $this->_option['gd_info']['gd_price'] = Tool_input::input('gd_price', 'P', 0);
        $this->_option['gd_info']['gd_url'] = Tool_input::input('gd_url', 'P', '');
        $this->_option['gd_info']['gd_pic'] = Tool_input::input('gd_pic', 'P', '');
        $this->_option['gd_info']['gd_pic_id'] = Tool_input::input('gd_pic_id', 'P', '');
        
        //是否同步到微博
        $this->_option['pb2weibo'] = Tool_input::input('pb2weibo','P', false);
        $this->_option['pb2weibo'] = (int)$this->_option['pb2weibo'];
        
        //单品rid
        $this->_option['content_rid'] = Tool_input::input('content_rid','P', '');
        $this->_uid = $this->__visitor['uid'];
    }
    
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam()
    {
        //$this->check_referer();
        
        //检查可写权限
        if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            return $this->api_output(ML_RCODE_NOACTIVE);
        }
        $this->check_option();
        $this->at_nick();
    }
    
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main(){
        $oGenerator = new ml_model_dbIdGenerator();
        $feed_rid = 0;
        $rs = $oGenerator->get_albumfeed_rid($this->_option['album_id'], &$feed_rid);
        
        if(!$rs)
            $this->api_output(ML_RCODE_HACK);
        
        $this->_feed_rid = $feed_rid;
        $result = $this->save_data();
        if(!$result)
            $this->api_output(ML_RCODE_HACK);
        
        //发送@给用户
        //if(empty($this->_at_nick))
            //ml_tool_sendMq::atme_in_content($this->_uid , $this->_feed_rid , array_values($this->_at_nick));
        
        $this->count_deal();
        
        // todo 发送到微博
        //if(!empty($this->_option['pb2weibo']))
            //$this->send_weibo();
        $bizFeed = new ml_biz_feed_albumfeed();
        $option['uid'] = $this->_uid;
        $option['data'] = array($result); 
        $bizFeed->initParam($option);
        $data = $bizFeed->create_html(false);
        $this->_private_action_log();
        $this->api_output(ML_RCODE_SUCC, $data);
    }
    
    /**
     * 添加行为日志
     */
    private function _private_action_log(){
        $ext = $this->_private_action_log_data();
        ml_tool_actionlog::add_other_action($this->_uid ,ML_SINAACTCODE_ALBUM_FEED_PUBLISH, $this->_feed_rid,0,$ext);
    }
    private function _private_action_log_data(){
        $data['filter'] = $this->_content_type;
        $data['album_id'] = $this->_option['album_id'];
        $data['pic_id'] = $this->_option['pic_id'];
        $data['txt_len'] = strlen($this->_option['content']);
        switch ($this->_content_type){
            case 1:
                break;
            case 2:
                break;
            case 4:
            case 6:
                $a = parse_url($this->_option['gd_info']['gd_url']);
                $data['longurl'] = urlencode($this->_option['gd_info']['gd_url']);
                $data['product_source'] = $a['host'];
                $data['price'] = $this->_option['gd_info']['gd_price'];
                break;
            default:
                break;
        }
        return $data;
    }
    
    /**
     * 处理统计数据资源
     */
    function count_deal(){
        $oRedisCount = new ml_model_rdsCount();
        //$rs_ufd = $oRedisCount->incrUserfeedcnt($this->_uid);
        $rs_afd = $oRedisCount->incrAlbumfeedCnt($this->_option['album_id']);
        $rs_pafd = $oRedisCount->incrPicAlbumfeedCnt($this->_option['album_id'], $this->_option['pic_id']);
        $oRedisAlbum = new ml_model_album_rdsAlbum();
        $rs_aur = $oRedisAlbum->set_album_users($this->_option['album_id'], $this->_uid);
        
    }
    /**
     * @发送到微博
     */
    function send_weibo(){
        $ser_data = array();
        $o3rd = new ml_model_db3rdService();
        $rs = $o3rd -> get3rdServiceUser($this->uid,1 ,&$ser_data);
        if($rs){
            $wb['access_token']=$ser_data['access_token'];
        }else{
            $session=$this->getSession();
            $wb['access_token']=$session->getVal ('access_token');
        }
        $wbcontent=urldecode($this->content);
        $wbgd_title=urldecode($this->gd_title);
        
    }
    /**
     * @保存数据
     */
    function save_data(){
        $data['albumfeed_rid'] = $this->_feed_rid;
        $data['uid'] = $this->_uid;
        $data['content'] = $this->_option['content'];
        $data['gd_info'] = empty($this->_option['gd_info']) ? '' : array_filter($this->_option['gd_info']);
        $data['class_id'] = $this->_option['class_id'];
        $data['album_id'] = $this->_option['album_id'];
        $data['pic_id'] = $this->_option['pic_id'];
        $data['content_rid'] = $this->_option['content_rid'];
        $data['time'] = date('Y-m-d H:i:s');
        $data['reply_cnt'] = 0;
        $oAlbumfeed= new ml_model_album_dbAlbumfeed();
        $rs = $oAlbumfeed->insert_content($data);
        if(!$rs)
            return false;
        return $data;
    }
    /**
     * @昵称的处理
     */
    function at_nick(){
        $aNicks = ml_tool_nick::findAllAtNick($this->content);
        if($aNicks)
        {
            $this->_at_nick = ml_tool_nick::nicks2uids($aNicks);
            $this->_option['gd_info']['nick2uid'] = $this->_at_nick;
        }
    }
    /**
     * @检测发布内容参数
     */
    protected  function _param_content(){
        
        $this->_option['content'] = preg_replace('/\s+/',' ', $this->_option['content']);
        
        if(empty($this->_option['content']))
            return false;
        
        $this->_option['content'] = ml_tool_formatContent::repalcelink ($this->_option['content']);
        
        if(Tool_string::str_width($this->_option['content']) > ML_DATARULE_CONTENT_MAXLEN)
            return false;
        if(ml_tool_keyword::haveSensitiveWord($this->_option['content']))
            return false;
        
        return true;
    }
    
    /**
     * @检测产品内容
     **/
    protected  function _param_gd_info(){
        $flag = 0;
        if(!empty($this->_option['gd_info']['gd_title'])){
            $flag++;
        }
        if(!empty($this->_option['gd_info']['gd_price'])){
            $flag++;
            $type_array['gd_price'] = ML_DATATYPE_FLOAT;
        }
        if(!empty($this->_option['gd_info']['gd_url'])){
            $flag++;
            $type_array['gd_url'] = ML_DATATYPE_URL;
        }
        if(!empty($this->_option['gd_info']['gd_pic'])){
            $flag++;
            $type_array['gd_pic'] = ML_DATATYPE_URL;
        }
        if($flag === 0){
            if(!$this->_private_check_gd_pic_id()){
                $this->api_output(ML_RCODE_PARAM, '', 'gd_pic_id');
            }
            return true;
        }
        if($flag<4)
            return false;
        
        if(empty($this->_option['gd_info']['gd_pic_id'])){
            $rs = $this->_private_gd_pic_id();
            if(!$rs){
                $this->api_output(ML_RCODE_PARAM, '', 'gd_pic_id');
            }
            $this->_content_type = $this->_content_type ? $this->_content_type : 6;
        }else{
            $this->_content_type = $this->_content_type ? $this->_content_type : 4;
        }
            
        $type_array['gd_pic_id'] = ML_DATATYPE_ALNUM;
        
        foreach($this->_option['gd_info'] as $key=>$value){
            $type = $type_array[$key];
            if(!empty($type)){
                $option = array('type'=>$type, 'is_must'=>true);
                if(!Tool_input::param_base($value, $option))
                    $this->api_output(ML_RCODE_PARAM, '', $key);
            }
        }
        return true;
    } 
    
    private function _private_check_gd_pic_id(){
        if(empty($this->_option['gd_info']['gd_pic_id'])){
            $this->_content_type = 1;
            $this->_option['gd_info'] = '';
            return true;
        }
        $option = array('type'=>ML_DATATYPE_ALNUM);
        $this->_content_type = 2;
        return Tool_input::param_base($this->_option['gd_info']['gd_pic_id'], $option);
    }
    private function _private_gd_pic_id(){
        $data['uid'] = $this->_uid;
        $data['imgurl'] = $this->_option['gd_info']['gd_pic'];
        $rs = Tool_http::post(ML_INTERFACE_FETCHIMG, $data);
        if(!$rs)
            return false;
        $result = json_decode($rs, true);
        if ($result['code'] == ML_RCODE_SUCC) {
            $this->_option['gd_info']['gd_pic_id'] = $result['data'];
        } else {
            return false;
        }
        return true;
    }
}

new aj_albumfeed_publish();
